import 'package:async/async.dart';
import 'package:boss/main.dart';
import 'package:boss/models/staff_sell_item.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/charts/bar/bar_chart.dart';
import 'package:boss/ui/widget/charts/util/charts_color_util.dart';
import 'package:boss/ui/widget/loader/loader.dart';
import 'package:boss/ui/widget/modal/modal.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';

Future showAnalysisDetailModal(
  BuildContext context, {
  @required String startTime,
  @required String endTime,
  @required int commodityOrMenu,
  @required int takeOutType,
  @required int sortType,
  @required int queryID,
  @required String name,
}) {
  return showModal(
    context,
    builder: (context) => _AnalysisDetailModal(
      startTime: startTime,
      endTime: endTime,
      commodityOrMenu: commodityOrMenu,
      takeOutType: takeOutType,
      sortType: sortType,
      queryID: queryID,
      name: name,
    ),
  );
}

class _AnalysisDetailModal extends StatefulWidget {
  final String startTime;
  final String endTime;
  final int commodityOrMenu;
  final int takeOutType;
  final int sortType;
  final int queryID;
  final String name;
  final int type;

  const _AnalysisDetailModal({
    this.startTime,
    this.endTime,
    this.commodityOrMenu,
    this.takeOutType,
    this.sortType,
    this.queryID,
    this.name,
    this.type,
  });

  @override
  __AnalysisDetailModalState createState() => __AnalysisDetailModalState();
}

class __AnalysisDetailModalState extends State<_AnalysisDetailModal> {
  List<StaffSellItem> data = [];

  List<BarData<num>> get chartsData {
    return data
        .map((item) =>
            BarData<num>(item.userName, widget.sortType == 0 ? item.totalFee / 100 : item.cellNum ?? 0, ChartsColorUtil.defaultColors[data.indexOf(item)]))
        .toList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 650.w,
      height: 520.w,
      padding: EdgeInsets.symmetric(
        horizontal: 20.w,
        vertical: 30.w,
      ),
      child: Loader(
        loadTask: loadTask,
        builder: (context) => Column(
          children: <Widget>[
            Container(
              alignment: Alignment.centerLeft,
              margin: EdgeInsets.only(
                bottom: 30.w,
              ),
              child: RichText(
                text: TextSpan(text: '${widget.name}${widget.commodityOrMenu == 1 ? '分类' : '菜品'}员工销售明细', style: TextStyleConstant().normal_1_28, children: [
                  TextSpan(
                    text: '  单位(${widget.sortType == 0 ? '元' : '份'})',
                    style: TextStyleConstant().normal_4_22,
                  ),
                ]),
              ),
            ),
            Expanded(
              child: BarChart<num>(
                width: double.infinity,
                height: double.infinity,
                data: chartsData,
                labelBuilder: (data) => '${data.value}',
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future<Result> loadTask() async {
    try {
      var res = await api.queryUserAndSortCommodityCellDate(
        startTime: widget.startTime,
        endTime: widget.endTime,
        commodityOrMenu: widget.commodityOrMenu,
        takeOutType: widget.takeOutType,
        sortType: widget.sortType,
        queryID: widget.queryID,
      );

      if (!res.successful()) {
        return Result.error(res.msg);
      }

      setState(() {
        data = res.data;
      });

      return Result.value([]);
    } catch (e) {
      return Result.error(e.toString());
    }
  }
}
